|
|
|
|
|
|
|
|
Your classes will also usually have several private variables, but only the public variables will be visible to the rest of your application when the object is created.  |
|
|
|
|
|
|
|
|
|
|
After you create an object from your class (you will see how shortly), you can use these properties just like those in a custom control: |
|
|
|
|
|
|
|
|
MyObject.FirstName = "Joe"
MyObject.LastName = "Smith"
Msgbox "Hello, " & MyObject.FirstName |
|
|
|
|
|
|
|
|
The drawback of using public variables for object properties is that no validation is performed on them. In the example, you can assign any values to the FirstName, LastName, and DateHired properties, as long as the value is of the correct type. |
|
|
|
|
|
|
|
|
Property procedures are more flexible than public variables because they offer the advantage of being able to execute code when someone accesses a property. Property procedures are written like functions, but to users of an object, they behave just like properties. The three types of property procedures are as follows: |
|
|
|
|
|
|
|
|
Property GetA function executed when the user reads the property value. |
|
|
|
|
|
|
|
|
Property LetA sub executed when the user writes the property value. |
|
|
|
|
|
|
|
|
Property SetA special case of Property Let, in which the value being passed to the subroutine is itself an object. |
|
|
|
|
|
|
|
|
You enter property procedures in the class module's Code window. To have Visual Basic create the skeleton of the property procedures, display the Add Procedure dialog box, shown in Figure 16.2, by selecting Add Procedure from the Tools menu. Suppose you want to add another property to the class, the Department property. In the Add Procedure dialog box, enter Department in the Name field, select Property for the procedure type, and click OK. |
|
|
|
|
|
|
|
|
Figure 16.2
You can use the Add Procedure
dialog box, accessible from the Tools
menu, to create many different procedure
types, including property procedures. |
|
|
|
|
|
|
|
|
Using the Add Procedure dialog box causes Visual Basic to add the framework for the Property Let and Property Get procedures. By default, VB assumes that your property is a variant. For the Department property, use a String data type, so you can just change the procedure definitions. The new skeleton procedures are pictured in Figure 16.3. |
|
|
|
|
|